package com.stockz.ui.swing.charting.view;

import java.awt.BorderLayout;
import java.util.List;

import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.stockz.core.model.Scrip;
import com.stockz.core.util.Global;
import com.stockz.ui.swing.charting.listener.StockChartPanelListener;
import com.stockz.ui.swing.charting.listener.StockChartViewListener;
import com.stockz.ui.swing.charting.util.RepaintStockChartPanelUtil;
import com.stockz.ui.swing.charting.util.UIChartingConstant;
import com.stockz.ui.swing.core.View;

public class StockChartView extends View{
	private static final long serialVersionUID = 748852749254325290L;

	private final StockChartPanel stockChartPanel;
	private final JScrollPane scrollPane;
	private final StockChartControlPanel stockChartControlPanel;
	private final List<Scrip> scripList;
	
	
	public StockChartView(List<Scrip> scripList){
		super("Chart");
		this.scripList = scripList;
		setFocusable(true);
		stockChartPanel = new StockChartPanel(this);
		stockChartControlPanel = new StockChartControlPanel(this,scripList);
		addFocusListener(StockChartViewListener.getInstance());
		addInternalFrameListener(StockChartViewListener.getInstance());
		
		StockChartPanelListener stockChartPanelListener = new StockChartPanelListener();
		stockChartPanel.addMouseListener(stockChartPanelListener);
		stockChartPanel.addMouseMotionListener(stockChartPanelListener);
		stockChartPanel.addMouseWheelListener(stockChartPanelListener);
		stockChartPanel.addKeyListener(stockChartPanelListener);
		stockChartPanel.addFocusListener(stockChartPanelListener);
		Global.put(UIChartingConstant.SelectedStockChartPanel,stockChartPanel);
		
		scrollPane = new JScrollPane(stockChartPanel);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
		scrollPane.getHorizontalScrollBar().setUnitIncrement(stockChartPanel.getStockChartPanelState().getTimeUnitSpace());
		scrollPane.getViewport().addChangeListener(new ChangeListener(){
			@Override
			public void stateChanged(ChangeEvent event) {
				RepaintStockChartPanelUtil.repaintStockChartPanel(getStockChartPanel());
			}
		});
		getContentPane().add(stockChartControlPanel,BorderLayout.NORTH);
		getContentPane().add(scrollPane,BorderLayout.CENTER);
/*		addComponentListener(new ComponentAdapter(){
			@Override
			public void componentResized(ComponentEvent e) {
				validate();
			}
		});
*/	}

	public StockChartPanel getStockChartPanel() {
		return stockChartPanel;
	}

	public JScrollPane getScrollPane() {
		return scrollPane;
	}

	public List<Scrip> getScripList() {
		return scripList;
	}
}
